package com.huikeportal.controller;

import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import com.huikeportal.model.ChangeGcParam;
import com.huikeportal.model.SiteAllInfo;
import com.huikeportal.model.SiteSelectParam;
import com.huikeportal.model.SquareChange;
import com.huikeportal.model.UserInfo;
import com.huikeportal.service.LoginService;
import com.huikeportal.service.SelectSiteService;

/**
 * 更换广场
 * 
 * @author winner
 * 
 */
@Controller
public class SquareChangeController {

	@Autowired
	private HttpSession session;

	@Autowired
	private LoginService loginService;

	@Autowired
	private SelectSiteService selectSiteService;

	@InitBinder("changeGcParam")
	public void initBinder(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("changeGcParam.");
	}

	@RequestMapping("/changeSquLoad")
	public String changeSquLoad(ChangeGcParam changeGcParam, ModelMap model)
			throws Exception {
		if ("comparativeAnalysis".equals(changeGcParam.getBb_controlTo())) {
			model.addAttribute("pageHead", "对比分析");
		} else if ("squareWeek".equals(changeGcParam.getBb_controlTo())) {
			model.addAttribute("pageHead", "星期分析");
		} else if ("squareWeather".equals(changeGcParam.getBb_controlTo())) {
			model.addAttribute("pageHead", "天气分析");
		} else if ("userAddLoad".equals(changeGcParam.getBb_controlTo())
				|| "userEditLoad".equals(changeGcParam.getBb_controlTo())) {
			model.addAttribute("pageHead", "用户管理");
		} else if ("brandAnalysis".equals(changeGcParam.getBb_controlTo())) {
			model.addAttribute("pageHead", "品牌分析");
		}
		// 初始化参数
		changeGcParam = initParam(changeGcParam);
		UserInfo userInfo = (UserInfo) session.getAttribute("sessionUserName");
		userInfo = loginService.getUserInfo(userInfo);
		String menuHtml = loginService.getMenuHtml(userInfo, changeGcParam
				.getSiteSelectParam().getPageFrom());
		model.addAttribute("menuHtml", menuHtml);// 左侧菜单html
		// 获取用户广场权限
		List<SiteAllInfo> gcList = loginService.getGcSiteList(userInfo);
		// 获取用户店铺权限
		List<SiteAllInfo> dpList = loginService.getDpSiteList(userInfo);
//		SquareChange squareChange = selectSiteService.squareChangeLoad(
//				changeGcParam, gcList, dpList);
//		model.addAttribute("squareChange", squareChange);// 页面初始化信息
		model.addAttribute("siteSelectParam", changeGcParam
				.getSiteSelectParam());// 原页面参数
		model.addAttribute("selectType", changeGcParam.getSelectType());// 选择类型：单选、多选
		model.addAttribute("backPage", changeGcParam.getBackPage());// 要返回的页面地址
		return "squareChange";
	}

	/**
	 * 初始化参数
	 * 
	 * @param changeGcParam
	 * @return
	 */
	private ChangeGcParam initParam(ChangeGcParam changeGcParam) {
		SiteSelectParam siteSelectParam = new SiteSelectParam();
		if (!"".equals(changeGcParam.getBb_controlTo())
				&& changeGcParam.getBb_controlTo() != null) {
			siteSelectParam.setControlTo(changeGcParam.getBb_controlTo());
		}
		if (!"".equals(changeGcParam.getBb_saveParam())
				&& changeGcParam.getBb_saveParam() != null) {
			siteSelectParam.setSaveParam(changeGcParam.getBb_saveParam());
		}
		if (!"".equals(changeGcParam.getBb_dpSiteId())
				&& changeGcParam.getBb_dpSiteId() != null) {
			siteSelectParam.setDpSiteId(changeGcParam.getBb_dpSiteId());
		}
		if (!"".equals(changeGcParam.getBb_gcSiteId())
				&& changeGcParam.getBb_gcSiteId() != null) {
			siteSelectParam.setGcSiteId(changeGcParam.getBb_gcSiteId());
		}
		if (!"".equals(changeGcParam.getBb_lcSiteId())
				&& changeGcParam.getBb_lcSiteId() != null) {
			siteSelectParam.setLcSiteId(changeGcParam.getBb_lcSiteId());
		}
		if (!"".equals(changeGcParam.getBb_pageFrom())
				&& changeGcParam.getBb_pageFrom() != null) {
			siteSelectParam.setPageFrom(changeGcParam.getBb_pageFrom());
		}
		if (!"".equals(changeGcParam.getBb_qySiteId())
				&& changeGcParam.getBb_qySiteId() != null) {
			siteSelectParam.setQySiteId(changeGcParam.getBb_qySiteId());
		}
		if (!"".equals(changeGcParam.getBb_selectType())
				&& changeGcParam.getBb_selectType() != null) {
			siteSelectParam.setSelectType(changeGcParam.getBb_selectType());
		}
		if (!"".equals(changeGcParam.getBb_siteString())
				&& changeGcParam.getBb_siteString() != null) {
			siteSelectParam.setSiteString(changeGcParam.getBb_siteString());
		}
		if (!"".equals(changeGcParam.getBb_note())
				&& changeGcParam.getBb_note() != null) {
			siteSelectParam.setNote(changeGcParam.getBb_note());
		}
		if (!"".equals(changeGcParam.getBb_choiceType())
				&& changeGcParam.getBb_choiceType() != null) {
			siteSelectParam.setChoiceType(changeGcParam.getBb_choiceType());
		}
		if (!"".equals(changeGcParam.getBb_rangeType())
				&& changeGcParam.getBb_rangeType() != null) {
			siteSelectParam.setRangeType(changeGcParam.getBb_rangeType());
		}
		if (!"".equals(changeGcParam.getTabId())
				&& changeGcParam.getTabId() != null) {
			siteSelectParam.setBackTabId(changeGcParam.getTabId());
		}
		changeGcParam.setSiteSelectParam(siteSelectParam);
		return changeGcParam;
	}
}
